﻿using System;
using System.Web;

namespace QuailDashboard.View.Web.RegExUrlMapping
{
    public class RegExUrlMappingBaseModule : IHttpModule
    {
        /// <summary>
        /// Initializes a module and prepares it to handle requests.
        /// </summary>
        /// <param name="context">An <see cref="T:System.Web.HttpApplication"/> that provides access to the methods, properties, and events common to all application objects within an ASP.NET application</param>
        public void Init(HttpApplication context)
        {
            context.AuthorizeRequest += BaseModuleRewriter_OnAuthorizeRequest;
        }

        /// <summary>
        /// Disposes of the resources (other than memory) used by the module that implements <see cref="T:System.Web.IHttpModule"/>.
        /// </summary>
        public void Dispose()
        {
        }

        /// <summary>
        /// Rewrites the url from a requested path for the given http application
        /// </summary>
        /// <param name="requestedPath">the requested path</param>
        /// <param name="app">the httpapplication</param>
        protected virtual void Rewrite(string requestedPath, HttpApplication app)
        {
        }

        private void BaseModuleRewriter_OnAuthorizeRequest(object sender, EventArgs e)
        {
            var app = sender as HttpApplication;

            if (app != null)
                Rewrite(app.Request.Path, app);
        }
    }
}
